package org.springblade.tms.module.monitor.base;

import org.springblade.core.tool.api.R;
import org.springblade.tms.global.entity.TimeInterval;
import org.springblade.tms.global.enums.UserTypeEnum;
import org.springblade.tms.global.enums.WaybillStatusEnum;
import org.springblade.tms.module.demandorder.entity.TMDemandOrder;
import org.springblade.tms.module.demandorder.service.IDemandOrderService;
import org.springblade.tms.module.exceptionrecord.entity.TMExceptionRecord;
import org.springblade.tms.module.exceptionrecord.service.IExceptionRecordService;
import org.springblade.tms.module.exceptiontype.entity.TMExceptionType;
import org.springblade.tms.module.exceptiontype.service.IExceptionTypeService;
import org.springblade.tms.module.monitor.vo.StatisticalParameters;
import org.springblade.tms.module.monitor.vo.TransportationStatisticsVo;
import org.springblade.tms.module.transportorder.entity.TMTransportOrder;
import org.springblade.tms.module.transportorder.service.ITransportOrderService;
import org.springblade.tms.module.userinfo.entity.TMUserInfo;
import org.springblade.tms.module.userinfo.service.IUserInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @projectName: BladeX
 * @package: org.springblade.tms.module.monitor.base
 * @className: MonitorService
 * @author: yr
 * @description: 监控服务
 * @date: 2022/3/18 13:45
 */
@Service
@Transactional
public class MonitorService {
	@Resource
	private IExceptionRecordService exceptionRecordService;
	@Resource
	private IExceptionTypeService exceptionTypeService;
	@Resource
	private IUserInfoService userInfoService;
	@Resource
	private IDemandOrderService demandOrderService;
	@Resource
	private ITransportOrderService transportOrderService;

	/**
	 * @param timeInterval:
	 * @return R
	 * @author yr
	 * @description 运输统计
	 * @date 2022/3/18 13:49
	 */
	public R<TransportationStatisticsVo> transportationStatistics(TimeInterval timeInterval) {
		if (timeInterval.getStart()==null){
			timeInterval.setEnd(LocalDateTime.now());
			timeInterval.setStart(LocalDateTime.now().minusDays(7));
		}
		//new返回值
		TransportationStatisticsVo transportationStatisticsVo = new TransportationStatisticsVo(new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
		//获取异常类型统计
		List<TMExceptionType> exceptionTypeList = exceptionTypeService.lambdaQuery().list();
		Map<Long, String> exceptionTypeMap = exceptionTypeList.stream().collect(Collectors.toMap(TMExceptionType::getId, TMExceptionType::getName));
		List<TMExceptionRecord> exceptionRecordList = exceptionRecordService.lambdaQuery()
			.between(timeInterval != null, TMExceptionRecord::getCreateTime, timeInterval.getStart(), timeInterval.getEnd()).list();
		Map<Long, List<TMExceptionRecord>> exceptionRecordMap = exceptionRecordList.stream().collect(Collectors.groupingBy(TMExceptionRecord::getExceptionType));
		HashMap<String, Integer> exceptionMap = new HashMap<>();
		if (exceptionTypeMap != null && exceptionRecordMap != null) {
			for (Long id : exceptionTypeMap.keySet()) {
				if (exceptionRecordMap.get(id) != null) {
					exceptionMap.put(exceptionTypeMap.get(id), exceptionRecordMap.get(id).size());
					transportationStatisticsVo.getTransportationAnomalyStatistics()
						.add(new StatisticalParameters(exceptionTypeMap.get(id),exceptionRecordMap.get(id).size()));
				}
			}
		}
		//获取所有承运商订单统计
		List<TMUserInfo> userInfoList = userInfoService.lambdaQuery().eq(TMUserInfo::getUserType, UserTypeEnum.CARRIER.getValue()).list();
		Map<String, String> userInfoMap = userInfoList.stream().collect(Collectors.toMap(TMUserInfo::getTmId, TMUserInfo::getName));
		List<TMDemandOrder> demandOrderList = demandOrderService.lambdaQuery()
			.between(timeInterval != null, TMDemandOrder::getCreateTime, timeInterval.getStart(), timeInterval.getEnd()).list();
		Map<String, List<TMDemandOrder>> tmDemandOrderMap = demandOrderList.stream().collect(Collectors.groupingBy(TMDemandOrder::getCarrierNo));
		HashMap<String, Integer> demandOrderMap = new HashMap<>();
		if (userInfoMap != null && tmDemandOrderMap != null) {
			for (String id : userInfoMap.keySet()) {
				if (tmDemandOrderMap.get(id) != null) {
					transportationStatisticsVo.getCarrierOrderStatistics()
						.add(new StatisticalParameters(userInfoMap.get(id),tmDemandOrderMap.get(id).size()));
				}
			}
		}
		//获取运单类型统计
		List<TMTransportOrder> transportOrderList = transportOrderService.lambdaQuery()
			.between(timeInterval != null, TMTransportOrder::getCreateTime, timeInterval.getStart(), timeInterval.getEnd()).list();
		Map<String, List<TMTransportOrder>> tmTransportOrderMap = transportOrderList.stream().collect(Collectors.groupingBy(TMTransportOrder::getTransportStatus));
		HashMap<String, Integer> TransportOrderMap = new HashMap<>();
		if (tmTransportOrderMap != null) {
			for (String id : tmTransportOrderMap.keySet()) {
				if (tmTransportOrderMap.get(id) != null) {
					TransportOrderMap.put(WaybillStatusEnum.getDescriptionByValue(id), tmTransportOrderMap.get(id).size());
					transportationStatisticsVo.getWaybillTypeStatistics()
						.add(new StatisticalParameters(WaybillStatusEnum.getDescriptionByValue(id), tmTransportOrderMap.get(id).size()));
				}
			}
		}
		return R.data(transportationStatisticsVo);
	}
}
